<?php
    /*

     Version: MPL 1.1

     Software distributed under the License is distributed on an "AS IS"
     basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
     License for the specific language governing rights and limitations
     under the License.

     The Original Code is KASSIOP Framework.

     The Initial Developer of the Original Code is SURIAN Nicolas (aka NairuS) <me@nairus.fr>.
     Portions created by the Initial Developer are Copyright (C) 2010
     The Initial Developer. All Rights Reserved.

     Contributor(s):

     Alternatively, the contents of this file may be used under the terms
     of the MPL license Version 1.1 (the  "MPL" License"), in which case the
     provisions of Version: MPL 1.1 License are applicable instead of those
     above.  If you wish to allow use of your version of this file only
     under the terms of the MPL License and not to allow others to use
     your version of this file under the MPL, indicate your decision by
     deleting  the provisions above and replace  them with the notice and
     other provisions required by the MPL License.  If you do not delete
     the provisions above, a recipient may use your version of this file
     under either the MPL License.

     The contents of this file are subject to the Mozilla Public License
     Version 1.1 (the "License"); you may not use this file except in
     compliance with the License. You may obtain a copy of the License at
     http://www.mozilla.org/MPL/

    */


    require_once 'PHPUnit/Framework.php';
    require_once dirname( __FILE__ ) . '/../../../../kassiop/core/Autoload.php' ;
    Autoload::register() ;

    /**
     * The unit test class of the BooleanValidator class.
     *
     * @package    unittests.kassiop.system
     * @subpackage validators
     *
     * @author NairuS
     * @version 1.3 $Rev: 90 $ $Date: 2011-01-16 09:23:26 +0000 (Sun, 16 Jan 2011) $ $Author: nicolas.surian $
     */
    class BooleanValidatorTest extends PHPUnit_Framework_TestCase
    {
        /**
         * Defines the validator to test.
         *
         * @var BooleanValidator
         */
        protected $validator ;

        protected function setUp()
        {
            $this->validator = new BooleanValidator() ;
        }

        protected function tearDown()
        {
            unset( $this->validator ) ;
        }

        public function testStaticProperties()
        {
            $this->assertEquals( 'true_values' , BooleanValidator::$trueValuesKey , '1. The static property must be well setted.' );
            $this->assertEquals( 'false_values', BooleanValidator::$falseValuesKey, '2. The static property must be well setted.' );
        }

        public function testClean()
        {
            $this->assertTrue( $this->validator->clean( true )  , '1.1 The validator result must be true.' ) ;
            $this->assertTrue( $this->validator->clean( 't' )   , '1.2 The validator result must be true.' ) ;
            $this->assertTrue( $this->validator->clean( 'yes' ) , '1.3 The validator result must be true.' ) ;
            $this->assertTrue( $this->validator->clean( 'y' )   , '1.4 The validator result must be true.' ) ;
            $this->assertTrue( $this->validator->clean( 'on' )  , '1.5 The validator result must be true.' ) ;
            $this->assertTrue( $this->validator->clean( '1' )   , '1.6 The validator result must be true.' ) ;
            $this->assertTrue( $this->validator->clean( 1 )     , '1.7 The validator result must be true.' ) ;
            $this->assertTrue( $this->validator->clean( 'true' ), '1.8 The validator result must be true.' ) ;

            $this->assertTrue( $this->validator->clean( false ) === 0  , '2.1 The validator result must be false.' ) ;
            $this->assertTrue( $this->validator->clean( 'false' ) === 0, '2.2 The validator result must be false.' ) ;
            $this->assertTrue( $this->validator->clean( 'f' ) === 0    , '2.3 The validator result must be false.' ) ;
            $this->assertTrue( $this->validator->clean( 'no' ) === 0   , '2.4 The validator result must be false.' ) ;
            $this->assertTrue( $this->validator->clean( 'n' ) === 0    , '2.5 The validator result must be false.' ) ;
            $this->assertTrue( $this->validator->clean( 'off' ) === 0  , '2.6 The validator result must be false.' ) ;
            $this->assertTrue( $this->validator->clean( '0' ) === 0    , '2.7 The validator result must be false.' ) ;
            $this->assertTrue( $this->validator->clean( 0 ) === 0      , '2.8 The validator result must be false.' ) ;

            try
            {
                $this->validator->clean( 'not_exists' );
            }
            catch( Exception $e )
            {
                $this->assertTrue( $e instanceof ErrorBaseValidator, 'The validator result must be an instance of ErrorBaseValidator.' ) ;
                $this->assertEquals( $e->getMessage(), '"not_exists" is not a valid boolean value.', 'The error message must be well formed.' ) ;
            }
        }

        public function testGetMessage()
        {
            $this->assertEquals( '"%value%" is not a valid boolean value.', $this->validator->getMessage( BooleanValidator::$invalidKey ) );
        }

        public function testGetOption()
        {
            $this->assertEquals( $this->validator->getOption( BooleanValidator::$trueValuesKey ), array( 'true', 't', 'yes', 'y', 'on', '1' ), 'The true values must be well setted.' ) ;
            $this->assertEquals( $this->validator->getOption( BooleanValidator::$falseValuesKey ), array( 'false', 'f', 'no', 'n', 'off', '0'), 'The false values must be well setted.' ) ;
        }
    }
?>